package com.example.administrator.himalaya.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.activity.Main2Activity;
import com.example.administrator.himalaya.activity.bendiActivity;
import com.example.administrator.himalaya.activity.frist_mostActivity;
import com.example.administrator.himalaya.activity.guojiaActivity;
import com.example.administrator.himalaya.activity.lishi_mostActivity;
import com.example.administrator.himalaya.activity.shengActivity;
import com.example.administrator.himalaya.activity.wangluoActivity;
import com.example.administrator.himalaya.adapter.MyAdapter2;
import com.example.administrator.himalaya.adapter.MyAdapter3;
import com.example.administrator.himalaya.entity.castentity.Entity;
import com.example.administrator.himalaya.live_button_activity.DushiActivity;
import com.example.administrator.himalaya.live_button_activity.FangyanActivity;
import com.example.administrator.himalaya.live_button_activity.JiaotongActivity;
import com.example.administrator.himalaya.live_button_activity.JingjiActivity;
import com.example.administrator.himalaya.live_button_activity.LvyouActivity;
import com.example.administrator.himalaya.live_button_activity.QitaActivity;
import com.example.administrator.himalaya.live_button_activity.QuyiActivity;
import com.example.administrator.himalaya.live_button_activity.ShenghuoActivity;
import com.example.administrator.himalaya.live_button_activity.TiyuActivity;
import com.example.administrator.himalaya.live_button_activity.WaiyuActivity;
import com.example.administrator.himalaya.live_button_activity.WenyiActivity;
import com.example.administrator.himalaya.live_button_activity.XiaoyuanActivity;
import com.example.administrator.himalaya.live_button_activity.XinwenActivity;
import com.example.administrator.himalaya.live_button_activity.YinyueActivity;
import com.example.administrator.himalaya.live_button_activity.ZongheActivity;
import com.example.administrator.himalaya.service.service;
import com.example.administrator.himalaya.sqlHelper.Helper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FragmentDicsoverLive extends Fragment implements Callback<Entity>, View.OnClickListener, AdapterView.OnItemClickListener {
    private List<Entity.DataEntity.CategoriesEntity> datas;
    private List<Entity.DataEntity.RecommandRadiosEntity> china_datas;
    private List<Entity.DataEntity.TopRadiosEntity> PHB_datas;
    private GridLayout mListView;
    private ListView mChinaList;
    private MyAdapter2 mAdapter2;
    private ListView mList_phb;
    private MyAdapter3 mAdapter3;
    private ListView mLS_listView;
    private GridLayout mLayout;
    private ImageButton text8, text16;
    private service mService;
    private RelativeLayout mFirst_most;
    private SimpleCursorAdapter mAdapterm;

    public FragmentDicsoverLive() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_for, container, false);
        View text1 = ret.findViewById(R.id.gl_text1);
        View text2 = ret.findViewById(R.id.gl_text2);
        View text3 = ret.findViewById(R.id.gl_text3);
        View text4 = ret.findViewById(R.id.gl_text4);
        View text5 = ret.findViewById(R.id.gl_text5);
        View text6 = ret.findViewById(R.id.gl_text6);
        View text7 = ret.findViewById(R.id.gl_text7);
        View text8 = ret.findViewById(R.id.gl_text8);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        text4.setOnClickListener(this);
        text5.setOnClickListener(this);
        text6.setOnClickListener(this);
        text7.setOnClickListener(this);
        text8.setOnClickListener(this);
        View text1_1 = ret.findViewById(R.id.gl_text1_1);
        View text2_1 = ret.findViewById(R.id.gl_text2_1);
        View text3_1 = ret.findViewById(R.id.gl_text3_1);
        View text4_1 = ret.findViewById(R.id.gl_text4_1);
        View text5_1 = ret.findViewById(R.id.gl_text5_1);
        View text6_1 = ret.findViewById(R.id.gl_text6_1);
        View text7_1 = ret.findViewById(R.id.gl_text7_1);
        View text8_1 = ret.findViewById(R.id.gl_text8_1);
        View text9_1 = ret.findViewById(R.id.gl_text9_1);
        View text10_1 = ret.findViewById(R.id.gl_text10_1);
        View text11_1 = ret.findViewById(R.id.gl_text11_1);
        View text12_1 = ret.findViewById(R.id.gl_text12_1);
        View text13_1 = ret.findViewById(R.id.gl_text13_1);
        View text14_1 = ret.findViewById(R.id.gl_text14_1);
        View text15_1 = ret.findViewById(R.id.gl_text15_1);
        text1_1.setOnClickListener(this);
        text2_1.setOnClickListener(this);
        text3_1.setOnClickListener(this);
        text4_1.setOnClickListener(this);
        text5_1.setOnClickListener(this);
        text6_1.setOnClickListener(this);
        text7_1.setOnClickListener(this);
        text8_1.setOnClickListener(this);
        text9_1.setOnClickListener(this);
        text10_1.setOnClickListener(this);
        text11_1.setOnClickListener(this);
        text12_1.setOnClickListener(this);
        text13_1.setOnClickListener(this);
        text14_1.setOnClickListener(this);
        text15_1.setOnClickListener(this);
        //        开始显示 4 * 2
        mLayout = (GridLayout) ret.findViewById(R.id.gl1);
//        隐藏  4 * 4
        mListView = (GridLayout) ret.findViewById(R.id.listView);
//        4 * 2
        text8 = (ImageButton) ret.findViewById(R.id.gl_text8);
        RelativeLayout lishi_most = (RelativeLayout) ret.findViewById(R.id.lishi_most);
        lishi_most.setOnClickListener(this);
//         4 * 4
        text16 = (ImageButton) ret.findViewById(R.id.gl_text16_1);
        mFirst_most = (RelativeLayout) ret.findViewById(R.id.first_most);
        mFirst_most.setOnClickListener(this);
        text8.setOnClickListener(this);
        text16.setOnClickListener(this);
        mChinaList = (ListView) ret.findViewById(R.id.china_list);
//        listView的点击事件
        mChinaList.setOnItemClickListener(this);


        mList_phb = (ListView) ret.findViewById(R.id.listView2);
        mList_phb.setOnItemClickListener(this);


        TextView textView = (TextView) ret.findViewById(R.id.two);
        textView.setOnClickListener(this);
        RelativeLayout Layout = (RelativeLayout) ret.findViewById(R.id.relative);
        Layout.setOnClickListener(this);
        TextView bendi = (TextView) ret.findViewById(R.id.discover_broadcast_bendi);
        bendi.setOnClickListener(this);
        TextView guojia = (TextView) ret.findViewById(R.id.discover_broadcast_guojia);
        guojia.setOnClickListener(this);
        TextView shengshi = (TextView) ret.findViewById(R.id.discover_broadcast_shengshi);
        shengshi.setOnClickListener(this);
        TextView wangluo = (TextView) ret.findViewById(R.id.discover_broadcast_wangluo);
        wangluo.setOnClickListener(this);
        mLS_listView = (ListView) ret.findViewById(R.id.LS_listView);
        ArrayList<String> list = new ArrayList<>();
//        数据库查询
        Helper helper = new Helper(getContext());
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(
                Helper.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int index = cursor.getColumnIndex("title");
                int index1 = cursor.getColumnIndex("name");
                int index2 = cursor.getColumnIndex("number");
                if (index != -1) {
                    String title = cursor.getString(index);
                    Log.d("1111", "title: " + title);
                }
                if (index1 != -1) {
                    String name = cursor.getString(index1);
                    Log.d("1111", "time: " + name);
                }
                if (index2 != -1) {
                    String number = cursor.getString(index2);
                    Log.d("1111", "name: " + number);
                }
                mAdapterm = new SimpleCursorAdapter(
                        getContext(),
                        R.layout.china,
                        cursor,
                        new String[]{"title", "name", "number"},
                        new int[]{R.id.china_title, R.id.china_ing, R.id.china_per},
                        SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
                );
                mLS_listView.setAdapter(mAdapterm);
            }
        }

        mService = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(service.class);
        mService.getList().enqueue(this);
        datas = new ArrayList<>();
        china_datas = new ArrayList<>();
        PHB_datas = new ArrayList<>();
        mAdapter2 = new MyAdapter2(getContext(), china_datas);
        mAdapter3 = new MyAdapter3(getContext(), PHB_datas);
        mChinaList.setAdapter(mAdapter2);
        mList_phb.setAdapter(mAdapter3);
        return ret;
    }

    //返回刷新：：：
    @Override
    public void onResume() {
        super.onResume();
        flash();
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.gl_text8:
                mLayout.setVisibility(View.GONE);
                mListView.setVisibility(View.VISIBLE);
                text16.setVisibility(View.VISIBLE);
                break;
            case R.id.gl_text16_1:
                mLayout.setVisibility(View.VISIBLE);
                text16.setVisibility(View.GONE);
                mListView.setVisibility(View.GONE);
                break;
            case R.id.relative:
                intent.setClass(getContext(), Main2Activity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.two:
                intent.setClass(getContext(), Main2Activity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.discover_broadcast_bendi:
                intent.setClass(getContext(), bendiActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.discover_broadcast_guojia:
                intent.setClass(getContext(), guojiaActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.discover_broadcast_shengshi:
                intent.setClass(getContext(), shengActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.discover_broadcast_wangluo:
                intent.setClass(getContext(), wangluoActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.first_most:
                intent.setClass(getContext(), frist_mostActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.lishi_most:
                intent.setClass(getContext(), lishi_mostActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text1:
            case R.id.gl_text1_1:
                intent.setClass(getContext(), XinwenActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text2:
            case R.id.gl_text2_1:
                intent.setClass(getContext(), YinyueActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text3:
            case R.id.gl_text3_1:
                intent.setClass(getContext(), JiaotongActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text4:
            case R.id.gl_text4_1:
                intent.setClass(getContext(), JingjiActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text5:
            case R.id.gl_text5_1:
                intent.setClass(getContext(), TiyuActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text6:
            case R.id.gl_text6_1:
                intent.setClass(getContext(), WenyiActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text7:
            case R.id.gl_text7_1:
                intent.setClass(getContext(), QuyiActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text8_1:
                intent.setClass(getContext(), ZongheActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text9_1:
                intent.setClass(getContext(), XiaoyuanActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text10_1:
                intent.setClass(getContext(), FangyanActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text11_1:
                intent.setClass(getContext(), WaiyuActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text12_1:
                intent.setClass(getContext(), ShenghuoActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text13_1:
                intent.setClass(getContext(), DushiActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text14_1:
                intent.setClass(getContext(), LvyouActivity.class);
                getContext().startActivity(intent);
                break;
            case R.id.gl_text15_1:
                intent.setClass(getContext(), QitaActivity.class);
                getContext().startActivity(intent);
                break;

        }
    }

    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
//        datas.addAll(response.body().getData().getCategories());
        china_datas.addAll(response.body().getData().getRecommandRadios());
        PHB_datas.addAll(response.body().getData().getTopRadios());
        mAdapter2.notifyDataSetChanged();
        mAdapter3.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<Entity> call, Throwable t) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mChinaList) {
//        第一个listView 添加数据库
            Helper helper = new Helper(getContext());
            SQLiteDatabase db = helper.getReadableDatabase();
            ContentValues values = new ContentValues();

            String title = china_datas.get(position).getName();
            String name = china_datas.get(position).getProgramName();
            SimpleDateFormat format = new SimpleDateFormat("yyyy" + "年" + "MM" + "月" + "dd" + "日" + "hh:mm:ss");
            String time = format.format(new Date());
            values.put("title", title);
            values.put("name", "上次收听的节目：" + name);
            values.put("number", "上次收听的时间：" + time);
            db.insert(Helper.TABLE_NAME, null, values);
            Toast.makeText(getContext(), "添加数据库成功", Toast.LENGTH_SHORT).show();
        } else {
            Helper helper = new Helper(getContext());
            SQLiteDatabase db = helper.getReadableDatabase();
            ContentValues values = new ContentValues();

            String title = PHB_datas.get(position).getName();
            String name = PHB_datas.get(position).getProgramName();
            SimpleDateFormat format = new SimpleDateFormat("yyyy" + "年" + "MM" + "月" + "dd" + "日" + "hh:mm:ss");
            String time = format.format(new Date());

            values.put("title", title);
            values.put("name", "上次收听的节目：" + name);
            values.put("number", "上次收听的时间：" + time);
            db.insert(Helper.TABLE_NAME, null, values);
            Toast.makeText(getContext(), "添加数据库成功", Toast.LENGTH_SHORT).show();
            flash();
        }
        flash();
    }

    public void flash() {
        Helper helper = new Helper(getContext());
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(
                Helper.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getContext(),
                R.layout.china,
                cursor,
                new String[]{"title", "name", "number"},
                new int[]{R.id.china_title, R.id.china_ing, R.id.china_per},
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
        );
        mLS_listView.setAdapter(adapter);
    }
}
